<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\UserModel;

class IndexController extends Controller
{

    public function index()
    {
        $UM = new UserModel();
        $FM = $UM->create();
        if (! $FM) {
            $this->validateMess = $UM->getError();
        } else {
            $verify = new \Think\Verify();
            $isverify = $verify->check($_POST['captcha']);
            if (! $isverify) {
                $this->validateMess = '验证码错误！';
            } else {
                $rs = login($FM);
                if (! ($rs['status'])) {
                    $this->validateMess = $rs['data'];
                } else {
                    $this->success('登录成功，正在跳转...', U('/Main/main'), 1);
                }
            }
        }
        
        $this->display();
    }

    public function verify()
    {
        $config = array(
            'fontSize' => 20, // 验证码字体大小
            'length' => 4, // 验证码位数
            'imageH' => 35, // 验证码高度
            'useNoise' => true, // 关闭验证码杂点
            'fontttf' => '1.ttf',
            'codeSet' => '0123456789',
            'useImgBg' => true
        );
        $verify = new \Think\Verify($config);
        $verify->entry();
    }

    public function logout()
    {sess();
        logout();
        $this->success("退出成功！", U('Index/index'));
    }

    public function upload()
    {
        $upload = new \Think\Upload();
        $upload->maxSize = C('UPLOAD_MAXSIZE');
        $upload->exts = C('UPLOAD_EXTS');
        $upload->savePath = C('UPLOAD_SAVEPATH');
        $info = $upload->upload();
        $gourl = 'index.php/home/public/attfile/attid/' . I('attid') . '/';
        if (! $info) {
            echo "<script language='javascript' type='text/javascript'>";
            echo "alert('上传失败！$upload->getError()');";
            echo "window.location.href='$gourl'";
            echo "</script>";
            // $this->error($upload->getError());
        } else {
            // dump($info);
            $data['attid'] = I('attid');
            $data['folder'] = 'Uploads/' . str_replace('./', '', $info["filename"]["savepath"]);
            $data['filename'] = $info["filename"]["savename"];
            $data['filetype'] = $info["filename"]["ext"];
            $data['filedesc'] = $info["filename"]["name"];
            $data['uid'] = session('uid');
            $data['addtime'] = date("Y-m-d H:i:s", time());
            // dump($data);
            M('files')->data($data)->add();
            $filename = $info["filename"]["name"];
            echo "<script language='javascript' type='text/javascript'>";
            echo "alert('文件$filename 上传成功');";
            echo "window.location.href='$gourl'";
            echo "</script>";
        }
    }
}